package com.milepost.workflow.flowdefform.ctrl;

import com.milepost.workflow.flowdefform.entity.FlowDefFormEntity;
import com.milepost.workflow.flowdefform.service.FlowDefFormService;
import com.milepost.workflow.response.Response;
import com.milepost.workflow.response.ResponseHelper;
import com.milepost.workflow.response.ReturnCode;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author huarf
 * @Description
 * @create 2022-03-26 15:05
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowDefForm")
public class FlowDefFormCtrl {

    private final FlowDefFormService flowDefFormService;


    /**
     * 查看流程表单
     * @param flowDefId
     * @return
     */
    @GetMapping("/getById")
    public Response<FlowDefFormEntity> getById(String flowDefId) {

        Response<FlowDefFormEntity> response = null;

        try {
            FlowDefFormEntity flowDefFormEntity = flowDefFormService.getByFlowDefId(flowDefId);
            response = ResponseHelper.createSuccessResponse(flowDefFormEntity);
        } catch (Exception e) {
            log.error("查看流程表单异常", e);
            response = ResponseHelper.createExceptionResponse(e);
        }

        return response;
    }


    /**
     * 创建流程表单
     *
     * @param flowDefFormEntity
     * @return
     */
    @PostMapping("/createFlowDefForm")
    public Response<?> createFlowDefForm(@RequestBody FlowDefFormEntity flowDefFormEntity) {

        Response<?> response = null;

        try {
            boolean success = flowDefFormService.createFlowDefForm(flowDefFormEntity);
            if (success) {
                response = ResponseHelper.createResponse(ReturnCode.SUCCESS, "创建流程表单成功");
            } else {
                response = ResponseHelper.createResponse(ReturnCode.FAIL, "创建流程表单失败");
            }
        } catch (Exception e) {
            log.error("创建流程表单异常", e);
            response = ResponseHelper.createExceptionResponse(e);
        }

        return response;
    }
}
